package com.gtt.lazy.util;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Hibernate模板工具类
 * 
 * @author ljb
 * @date 2009-7-7
 */
public class HibernateTemplateUtil extends HibernateDaoSupport {

	/**
	 * 实体对象存储
	 * 
	 * @param entity
	 * @return
	 */
	public Serializable save(Object entity) {
		return getHibernateTemplate().save(entity);
	}

	/**
	 * 实体对象更新
	 * 
	 * @param entity
	 */
	public void update(Object entity) {
		getHibernateTemplate().update(entity);
	}

	/**
	 * 有参数的实体更新
	 * 
	 * @param hql
	 * @param objects
	 * @param types
	 * @return
	 */
	public int update(final String hql, final Object[] objects,
			final Type[] types) {
		return (Integer) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						int updateRows = 0;
						Query query = session.createQuery(hql);
						updateRows = query.setParameters(objects, types)
								.executeUpdate();
						return updateRows;
					}
				});
	}

	/**
	 * 根据<code>entity</code>删除对象
	 * 
	 * @param entity
	 */
	public void delete(Object entity) {
		getHibernateTemplate().delete(entity);
	}

	/**
	 * 根据<code>id</code>删除对象
	 * 
	 * @param entityClass
	 *            实体class
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public int delete(final Class entityClass, final int id) {
		return (Integer) getHibernateTemplate().execute(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						int updateRows = 0;
						String hql = "delete from " + entityClass.getName()
								+ " where id = :id";
						Query query = session.createQuery(hql);
						updateRows = query.setInteger("id", new Integer(id))
								.executeUpdate();
						return updateRows;
					}
				});

	}

	/**
	 * 根据<code>id</code>和<code>entityClass</code>获得对象
	 * 
	 * @param entityClass
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Object get(Class entityClass, Serializable id) {
		return getHibernateTemplate().get(entityClass, id);
	}

	/**
	 * 查询（根据hql语句查询，没有分页，没有参数）
	 * 
	 * @param hql
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List find(String hql) {
		return getHibernateTemplate().find(hql);
	}

	/**
	 * 查询（根据sql语句查询，没有分页，没有参数）
	 * 
	 * @param sql
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findBySql(final String sql) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery(sql);
				return query.list();
			}
		});
	}

	/**
	 * 查询（根据sql语句查询，没有分页，没有参数）
	 * 
	 * @param sql
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findBySql(final String sql, final String alias,
			final Class clazz) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery(sql);
				return query.addEntity(alias, clazz).list();
			}
		});
	}

	/**
	 * 查询（根据sql语句查询，没有分页，没有参数）
	 * 
	 * @param sql
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findBySql(final String sql, final String alias,
			final Class clazz, final int firstResult, final int maxResults) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				return query.addEntity(alias, clazz).list();
			}
		});
	}

	/**
	 * 查询（根据hql查询，没有分页，有参数）
	 * 
	 * @param hql
	 * @param objects
	 * @param types
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List find(final String hql, final Object[] objects,
			final Type[] types) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setParameters(objects, types);
				return query.list();
			}
		});
	}

	/**
	 * 查询（根据hql查询，有分页，有参数）
	 * 
	 * @param hql
	 * @param parameters
	 * @param types
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List find(final String hql, final Object[] objects,
			final Type[] types, final int firstResult, final int maxResults) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setParameters(objects, types);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				return query.list();
			}
		});

	}

	/**
	 * 查询（根据hql查询，有分页，没有有参数）
	 * 
	 * @param hql
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List find(final String hql, final int firstResult,
			final int maxResults) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				return query.list();
			}
		});

	}

}
